package org.hamster.multidropbox.main.mediator
{
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.hamster.common.puremvc.mediator.BaseMediator;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.main.view.AppLoadingMask;
	import org.hamster.multidropbox.main.vo.AppLoadingMaskVO;
	import org.puremvc.as3.interfaces.INotification;
	
	public class AppLoadingMaskMediator extends BaseMediator
	{
		public static const NAME:String = "org.hamster.multidropbox.main.mediator.AppLoadingMaskMediator";
		
		private const _pairList:ArrayCollection = new ArrayCollection();
		private var   _notificationCache:Array = new Array();
		
		private var popup:AppLoadingMask;
		
		public function AppLoadingMaskMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			popup = new AppLoadingMask();
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				AppDropboxEvent.DROPBOX_LINK_FOLDERS_REQUEST,
				AppDropboxEvent.DROPBOX_LINK_FOLDERS_RESULT,
				AppDropboxEvent.DROPBOX_LINK_FOLDERS_FAULT
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var notificationName:String = notification.getName();
			if (this.isShowExisting(notificationName)) {
				var vo:AppLoadingMaskVO = this.getPairByShowNotification(notificationName);
				popup.label = vo.label;
				PopUpManager.addPopUp(popup, app, true);
				PopUpManager.centerPopUp(popup);
			} else if (this.isHideExisting(notificationName)) {
				PopUpManager.removePopUp(popup);
			}
		}
		
		public function addPair(showNotification:String, hideNotification:String, label:String):void
		{
			if (getPair(showNotification, hideNotification) == null) {
				this._pairList.addItem(new AppLoadingMaskVO(label, showNotification, hideNotification));
				buildNotificationCache();
			}
 		}
		
		public function removePair(showNotification:String, hideNotification:String):void
		{
			var tarPair:Object = getPair(showNotification, hideNotification);
			if (tarPair != null) {
				_pairList.removeItemAt(_pairList.getItemIndex(tarPair));
				buildNotificationCache();
			}
		}
		
		public function isShowExisting(showNotification:String):Boolean
		{
			for each (var pair:AppLoadingMaskVO in this._pairList) {
				if (pair.showNotification == showNotification) {
					return true;
				}
			}
			return false;
		}
		
		public function isHideExisting(hideNotification:String):Boolean
		{
			for each (var pair:AppLoadingMaskVO in this._pairList) {
				if (pair.hideNotification == hideNotification) {
					return true;
				}
			}
			return false;
		}
		
		public function getPair(showNotification:String, hideNotification:String):AppLoadingMaskVO
		{
			for each (var pair:AppLoadingMaskVO in this._pairList) {
				if (pair.showNotification == showNotification
					&& pair.hideNotification == hideNotification) {
					return pair;
				}
			}
			return null;
		}
		
		public function getPairByShowNotification(showNotification:String):AppLoadingMaskVO
		{
			for each (var pair:AppLoadingMaskVO in this._pairList) {
				if (pair.showNotification == showNotification) {
					return pair;
				}
			}
			return null;
		}
		
		public function buildNotificationCache():void
		{
			_notificationCache = new Array();
			for each (var pair:AppLoadingMaskVO in this._pairList) {
				_notificationCache.push(pair.showNotification);
				_notificationCache.push(pair.hideNotification);
			}
		}
		
		public function get app():MultiDropbox
		{
			return viewComponent as MultiDropbox;
		}
	}
}